<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.groupProdId ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        label="活动商品id"
        prop="groupProdId"
      >
        <el-input v-model="dataForm.groupProdId" />
      </el-form-item>
      <el-form-item
        label="商城id"
        prop="shopId"
      >
        <el-input v-model="dataForm.shopId" />
      </el-form-item>
      <el-form-item
        label="活动id"
        prop="groupActivityId"
      >
        <el-input v-model="dataForm.groupActivityId" />
      </el-form-item>
      <el-form-item
        label="商品id"
        prop="prodId"
      >
        <el-input v-model="dataForm.prodId" />
      </el-form-item>
      <el-form-item
        label="创建时间"
        prop="createTime"
      >
        <el-input v-model="dataForm.createTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshDataList'])
const dataRule = {}

const dataForm = ref({
  groupProdId: null,
  shopId: null,
  groupActivityId: null,
  prodId: null,
  createTime: null
})
const dataFormRef = ref(null)
const visible = ref(false)
const init = (groupProdId) => {
  dataForm.value.groupProdId = groupProdId || 0
  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.groupProdId) {
      http({
        url: http.adornUrl('/admin/groupProd/info/' + dataForm.value.groupProdId),
        method: 'get',
        params: http.adornParams()
      })
        .then(({ data }) => {
          dataForm.value = data
        })
    }
  })
}
defineExpose({ init })

/**
 * 表单提交
 */
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/groupProd'),
        method: dataForm.value.groupProdId ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      })
        .then(() => {
          ElMessage({
            message: $t('remindPop.success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              visible.value = false
              emit('refreshDataList')
            }
          })
        })
    }
  })
}

</script>
